package com.thermal.thermal_inspection.controller;

import com.mysql.cj.log.Log;
import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.InspectionData;
import com.thermal.thermal_inspection.entity.InspectionGroup;
import com.thermal.thermal_inspection.entity.InspectionGroupDetail;
import com.thermal.thermal_inspection.service.InspectionGroupService;
import com.thermal.thermal_inspection.service.InspectionService;
import com.thermal.thermal_inspection.vo.AddInspectionDataVO;
import com.thermal.thermal_inspection.vo.InspectionGroupVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/inspection/data")
public class InspectionGroupController {
    private static final Logger logger = Logger.getLogger(ClassController.class);

    @Autowired
    private InspectionGroupService inspectionGroupService;
    @Autowired
    private InspectionService inspectionService;
    /**
     * 新增巡检任务数据组
     */
    @PostMapping("")
    public CommonResponse<InspectionGroup> addInspectionDataGroup(@RequestBody AddInspectionDataVO addInspectionDataVO){
        System.out.println(addInspectionDataVO);
        if (inspectionGroupService.getInspectionGroupByGroupName(addInspectionDataVO.getInspectionGroupName()) != null){
            logger.error("巡检任务数据组名重复！");
            return CommonResponse.createForError("巡检任务数据组名重复！");
        }
        InspectionGroup inspectionGroup = inspectionGroupService.addInspectionGroup(addInspectionDataVO);
        if (inspectionGroup == null){
            logger.error("新建巡检任务数据组失败！");
            return CommonResponse.createForError("新建巡检任务数据组失败！");
        }
        else{
            logger.info("新建巡检任务数据组成功！");
            return CommonResponse.createForSuccess(inspectionGroup);
        }
    }
    /**
     * 为已有数据组新增数据
     */
    @PostMapping("/{inspectionGroupId}")
    public CommonResponse<String> addInspectionGroupDetail(@RequestBody InspectionGroupDetail inspectionGroupDetail
                                                            ,@PathVariable int inspectionGroupId){
        if (inspectionGroupService.getInspectionGroupByInspectionId(inspectionGroupId) == null){
            logger.error("数据组不存在！");
            return CommonResponse.createForError("数据组不存在！");
        }
        inspectionGroupDetail.setInspectionGroupId(inspectionGroupId);
        Integer isWrong = inspectionGroupService.addInspectionGroupDetail(inspectionGroupDetail);
        if (isWrong < 1){
            logger.error("新建数据失败！");
            return CommonResponse.createForError("新建数据失败！");
        }else {
            logger.info("新建数据成功！");
            return CommonResponse.createForSuccess("新建数据成功！");
        }
    }
    /**
     * 获取巡检任务数据组列表
     */
    @GetMapping("")
    public CommonResponse<List<InspectionGroupVo>> getInspectionGroups(){
        List<InspectionGroupVo> inspectionGroupVos = inspectionGroupService.getInspectionGroups();
        if (inspectionGroupVos == null){
            logger.error("获取巡检任务数据组列表失败！");
            return CommonResponse.createForError("获取巡检任务数据组列表失败！");
        }
        else{
            logger.info("获取巡检任务数据组列表成功！");
            return CommonResponse.createForSuccess(inspectionGroupVos);
        }
    }
    /**
     * 获取巡检任务数据组详情列表
     */
    @GetMapping("/{inspectionGroupId}")
    public CommonResponse<List<InspectionGroupDetail>> getInspectionGroupDetails(@PathVariable int inspectionGroupId){
        List<InspectionGroupDetail> inspectionGroupDetails = inspectionGroupService.getInspectionGroupDetails(inspectionGroupId);
        if (inspectionGroupDetails == null){
            logger.error("获取巡检任务数据组详情列表失败！");
            return CommonResponse.createForError("获取巡检任务数据组详情列表失败！");
        }
        else{
            logger.info("获取巡检任务数据组详情列表成功！");
            return CommonResponse.createForSuccess(inspectionGroupDetails);
        }
    }
    /**
     * 修改巡检任务数据组名
     */
    @PutMapping("")
    public CommonResponse<String> updateInspectionGroupName(@RequestBody InspectionGroup inspectionGroup){
        Integer isWrong = inspectionGroupService.updateInspectionGroup(inspectionGroup);
        if (isWrong < 1){
            logger.error("修改数据组名失败！");
            return CommonResponse.createForError("修改数据组名失败！");
        }
        else {
            logger.info("修改数据组名成功！");
            return CommonResponse.createForSuccess("修改数据组名成功！");
        }
    }
    /**
     * 删除数据组
     */
    @DeleteMapping("/{inspectionGroupId}")
    public CommonResponse<String> deleteInspectionGroup(@PathVariable int inspectionGroupId){
        String selectMsg = inspectionService.isUseGroup(inspectionGroupId);
        if (selectMsg != null)
            return CommonResponse.createForError(selectMsg);
        Integer isWrong = inspectionGroupService.deleteInspectionGroup(inspectionGroupId);
        if (isWrong < 1){
            logger.error("删除数据组失败！");
            return CommonResponse.createForError("删除数据组失败！");
        }
        else {
            logger.info("删除数据组成功！");
            return CommonResponse.createForSuccess("删除数据组成功！");
        }
    }
    /**
     * 删除数据组详情
     */
    @DeleteMapping("/detail/{groupDetailId}")
    public CommonResponse<String>  deleteInspectionGroupDetail(@PathVariable int groupDetailId){
        Integer isWrong = inspectionGroupService.deleteInspectionGroupDetail(groupDetailId);
        if (isWrong < 1){
            logger.error("删除数据组详情失败！");
            return CommonResponse.createForError("删除数据组详情失败！");
        }
        else {
            logger.info("删除数据组详情成功！");
            return CommonResponse.createForSuccess("删除数据组详情成功！");
        }
    }
}
